home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / daten / ispell / source / addons / xspell.shar / look.c < prev    next >
C/C++ Source or Header  |  1995-01-23  |  6KB  |  281 lines

  1. static char *progid = "look 1.0a, 4 December 92 philipp@res.enst.fr";
  2.  
  3. /*
  4.  * Xspell: Philippe-Andre Prindeville, Telecom Paris 4 December 1992
  5.  *
  6.  *    After cursing ispell numerous times for not having a
  7.  *    simple command line interface like "look" for searching
  8.  *    for words, I wrote one.  Most of the code was recycled
  9.  *    from Xspell anyway (I should plug it here but...).
  10.  *
  11.  *    You type:
  12.  *
  13.  *        look [-d dictionary] word1 [word2 ... wordn ]
  14.  *
  15.  *    and, in the best of UNIX tradition, it will *not* print
  16.  *    out the word if it likes it, will print out a list of
  17.  *    alternatives for dubious words, and will print a stupid
  18.  *    message (well after 10 minutes hacking I wasn't going
  19.  *    to spend 3 hours worrying about "user-friendly" error
  20.  *    messages) saying it didn't like the word...
  21.  *
  22.  *    Debugging is simple.  If you have patches, by all means
  23.  *    send them to me.
  24.  *
  25.  *    Enjoy,
  26.  *                            -Philip
  27.  */
  28.  
  29. #include "config.h"
  30. #include "ispell.h"
  31.  
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36.  
  37. #ifdef    DEBUG
  38. #define    dprintf        printf
  39. #define    dputc        putchar
  40. #else
  41. #define    dprintf        _dprintf
  42. #define    dputc(x)
  43. #endif
  44.  
  45. extern char    *optarg;
  46. extern int    optind, opterr, optopt;
  47.  
  48. char    *lang = "english";
  49. char    *fmt = NULL;
  50. int    verbose = 0;
  51.  
  52. static FILE    *srvin, *srvout;
  53. static char    possibilities[MAXPOSSIBLE][INPUTWORDLEN + MAXAFFIXLEN];
  54. static int    pcount;
  55.  
  56. static void
  57. Connect()
  58. {
  59.     int        srv[2], clnt[2];
  60.     int        pid, n, argc;
  61.     char     buf[BUFSIZ];
  62.     char    *argv[10];
  63.  
  64.     pipe(srv);
  65.     pipe(clnt);
  66.  
  67.     if ((pid = fork()) == -1) {
  68.     perror("fork");
  69.     exit(1);
  70.     } else if (! pid) {
  71.     dup2(srv[0], 0);
  72.     close(srv[0]); close(srv[1]);
  73.     dup2(clnt[1], 1);
  74.     close(clnt[0]); close(clnt[1]);
  75.  
  76.     argc = 0;
  77.     argv[argc++] = "ispell";
  78.     argv[argc++] = "-d";
  79.     argv[argc++] = lang;
  80.     if (! strcmp(lang, "french"))
  81.         argv[argc++] = "-Tlatin1";
  82. #if 0
  83.     argv[argc++] = "-P";
  84. #endif
  85.     argv[argc++] = "-S";
  86.     argv[argc++] = "-a";
  87.     argv[argc++] = NULL;
  88.  
  89.     execvp("ispell", argv);
  90.     perror("execl: ispell");
  91.     exit(1);
  92.     } else {
  93.     close(clnt[1]);
  94.     close(srv[0]);
  95.     if (! (srvin = fdopen(clnt[0], "r"))
  96.      || ! (srvout = fdopen(srv[1], "w"))) {
  97.         fprintf(stderr, "fdopen: cannot open stream\n");
  98.         exit(1);
  99.     }
  100.     /*
  101.      * get hello banner
  102.      */
  103.     n = fgets(buf, sizeof(buf) - 1, srvin);
  104.     dprintf(buf);
  105.  
  106.     if (verbose)
  107.         fputs(buf, stdout);
  108.     }
  109. }
  110.  
  111. static void
  112. SessionEnd()
  113. {
  114.     dprintf(">>> #\n");
  115.     fprintf(srvout, "#\n");
  116.     fclose(srvout);
  117.     fclose(srvin);
  118. }
  119.  
  120. static void
  121. Choices (word, count, poss)
  122. char    *word;
  123. int    count;
  124. char    poss[MAXPOSSIBLE][INPUTWORDLEN + MAXAFFIXLEN];
  125. {
  126.     int    i;
  127.  
  128.     printf("%s:", word);
  129.     for (i = 0; i < count; ++i)
  130.     if (! strpbrk(poss[i], " -")) printf(" %s", poss[i]);
  131.     putchar('\n');
  132. }
  133.  
  134. static void
  135. Clueless (word)
  136. char    *word;
  137. {
  138.     printf("%s ??? Totally bogus, bro'\n", word);
  139. }
  140.  
  141. static void
  142. ReadBack ()
  143. {
  144.     int        c, m, n, reliable;
  145.     long    pos;
  146.     char    word[INPUTWORDLEN];
  147.  
  148.     for (; ; ) {
  149.     dprintf("<<< ");
  150.     c = fgetc(srvin);
  151.     switch (c) {
  152.     case EOF:        /* very unexpected */
  153.         dprintf("EOF\n");
  154.         return;
  155.  
  156.     case '\n':        /* done! */
  157.         dprintf("\\n\n");
  158.         break;
  159.  
  160.     case '*':        /* word is OK */
  161.         dputc('*');
  162.         c = fgetc(srvin);
  163.         dputc(c);
  164.         break;
  165.  
  166.     case '+':        /* found derivative */
  167.         dputc('+');
  168.         (void)fgets(word, sizeof(word), srvin);
  169.         dprintf("%s", word);
  170.         break;
  171.  
  172.     case '&':        /* word is dubious */
  173.         dputc('&');
  174.         n = fscanf(srvin, " %s %d %d", word, &reliable, &pos);
  175.         pos--;        /* correct for uparrow we added */
  176.         dprintf("\"%s\" at %d, %d choices:", word,
  177.         pos, reliable);
  178.         for (n = 0; ; ++n) {
  179.         c = fgetc(srvin);    /* eat colon or comma */
  180.         if (c == '\n') break;
  181.         fscanf(srvin, " %[^,\n]", possibilities[n]);
  182.         dprintf(" \"%s\"", possibilities[n]); fflush(stdout);
  183.         }
  184.  
  185.         /* newline already eaten */
  186.         dputc(c);
  187.         pcount = n;
  188.         Choices(word, pcount, possibilities);
  189.         break;
  190.  
  191.     case '?':
  192.         dputc('?');
  193.         n = fscanf(srvin, " %s %d %d", word, &reliable, &pos);
  194.         pos--;        /* correct for uparrow we added */
  195.         dprintf(" \"%s\" at %d, %d choices:",
  196.         word, pos, reliable);
  197.         for (n = 0; ; ++n) {
  198.         c = fgetc(srvin);    /* eat colon or comma */
  199.         if (c == '\n') break;
  200.         fscanf(srvin, " %[^,\n]", possibilities[n]);
  201.         dprintf(" \"%s\"", possibilities[n]); fflush(stdout);
  202.         }
  203.         /* newline already eaten */
  204.         dputc('\n');
  205.         pcount = n;
  206.         Choices(word, pcount, possibilities);
  207.         break;
  208.  
  209.     case '#':
  210.         dputc('#');
  211.         n = fscanf(srvin, " %s %d", word, &pos);
  212.         pos--;        /* correct for uparrow we added */
  213.         dprintf(" \"%s\" at %d\n", word, pos);
  214.         fgetc(srvin);
  215.         pcount = 0;
  216.         Clueless(word);
  217.         break;
  218.  
  219.     case '-':        /* gak! */
  220.     default:
  221.         dputc(c);
  222.         fgets(word, sizeof(word), srvin);
  223.         dprintf(" %s", word);
  224.         fprintf(stderr, "Error: read '%c' on connection\n", c);
  225.         exit(1);
  226.         break;
  227.     }
  228.     }
  229. }
  230.  
  231. main(argc, argv)
  232. char *argv[];
  233. {
  234.     int        i, c;
  235.     char    *myname, *slash;
  236.     char    *tmp;
  237.  
  238.     myname = argv[0];
  239.     if (slash = strrchr(myname, '/'))
  240.     myname = ++slash;
  241.  
  242.     while ((c = getopt(argc, argv, "vd:")) != EOF)
  243.     switch (c) {
  244.     case 'd':        /* dictionary */
  245.         lang = optarg;
  246.         break;
  247.     case 'v':
  248.         ++verbose;
  249.         break;
  250.     case '?':
  251. usage:        fprintf(stderr, "usage: %s [-v] [-d dictionary] word1 [word2 ... wordn]\n", myname);
  252.         exit(1);
  253.     }
  254.  
  255.     if (!verbose && optind == argc) goto usage;
  256.  
  257.     Connect();
  258.     dprintf(">>> ^");
  259.     fputc('^', srvout);
  260.  
  261.     for (i = optind; i < argc; ++i) {
  262.     dprintf(" %s", argv[i]);
  263.     fprintf(srvout, " %s", argv[i]);
  264.     }
  265.     dputc('\n');
  266.     fputc('\n', srvout);
  267.     fclose(srvout);
  268.  
  269.     ReadBack();
  270.  
  271.     SessionEnd();
  272.  
  273.     exit(0);
  274. }
  275.  
  276. #ifndef    DEBUG
  277. dprintf()
  278. {
  279. }
  280. #endif
  281.